#!/usr/bin/python
###############################################################################
#                                                                             #
#    Copyright 2006,2007 Danil Dotsenko                                       #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################                                   #
#             Danil Dotsenko, dd@accentsolution.com                           #

#from __future__ import division
import karambino

def debug():
	return False

###########################################################################
# applet settings go here, and are accessible from everywhere as global
class configClass:
	'''
	An all-encoupasing, application-wide configuration storage location.
	This will be shared even between concurrently running themes.
	This is the equivalent of root directory of Windows registry.
	'''
	def __init__(self):
		self.common={
			'name':'Karambino Armatura infrastructure example',
			'version':"v1, March 25, 2007",
			'author':"Danil (Suslik) Dotsenko (dd@accentsolution.com)",
			'cleanStart':False,
			'interval':10000,
			}
		self.lang = None
		self.objects = {}
#
##########################################################

def setupObjects(widget):

	karambino.resizeWidget(widget, 120, 120)

	parent = karambino.imageClass('blue.png', 10, 10, 100, 100, widget=widget)
	parent.kid = karambino.imageClass('red.png', h=50, w=50, parent=parent)
	parent.kid.move(0,0,'center','center')
	parent.kid.hide()
	
	def mymousein(*args):
		try:
			x,y,button,obj,other = args[0],args[1],args[2],args[3],args[4:]
		except:
			x,y,button,obj = None, None, None, None
		if obj:
			obj.kid.show()

	def mymouseout(*args):
		try:
			x,y,button,obj,other = args[0],args[1],args[2],args[3],args[4:]
		except:
			x,y,button,obj = None, None, None, None
		if obj:
			obj.kid.hide()

	parent.mousein.signalset('inny',mymousein,parent)
	parent.mouseout.signalset('outy',mymouseout,parent)

##################################################################
# Testing & helper functions.
#
def notificationWindow(message, title="", mode=None):
	'''
	This uses KDE kdialog app and asks for main body message and window title.
	App name is attached automatically
	'''
	global appConfig
	myCommand = 'kdialog --caption "'+appConfig.common['name']+'" --title "'+title+'" --msgbox "'+message+'"'
	karambino.execute(myCommand)
#
##################################################################

##################################################################
# KARAMBA's built-in functions
#
#this is called when you widget is initialized
def initWidget(widget):
	global appConfig
	
	appConfig=configClass()
	appConfig.common['cleanStart'] = True
	# createMenus(widget)
	setupObjects(widget)

	karambino.redrawWidget(widget)

def widgetUpdated(widget):
	global appConfig
	if appConfig.common['cleanStart']:
		karambino.changeInterval(widget, appConfig.common['interval'])

def widgetClosed(widget):
	global appConfig
	try:
		if appConfig.common['cleanStart']:
			# put working stuff here
			pass
	except:
		pass

def itemDropped(widget, dropText, x=0, y=0):
	print "in the itemDrag dropText is ", dropText, type(dropText)

#  botton = button clicked:
#                    1 = Left Mouse Button
#                    2 = Middle Mouse Button
#                    3 = Right Mouse Button
#                    4,5 = Scroll wheel up and down
def widgetClicked(widget, x, y, button):
	#print "widgetClicked function, button is ", button
	pass

# NOTE you must use attachClickArea() to make a meterClicked work
def meterClicked(widget, meter, button):
	karambino.meterClicked(widget, meter, button)

def keyPressed(widget, meter, char):
	karambino.keyPressed(widget, meter, char)

def themeNotify(widget, caller, message):
	pass
	
def widgetMouseMoved(widget, x, y, button):
	#Warning:  Don't do anything too intensive here
	karambino.widgetMouseMoved(widget, x, y, button)

# This will be printed when the widget loads.
print "Karambino example is loaded."
